package com.hmkj.core.api.juhe.sdk;


import com.alibaba.fastjson.JSON;
import com.hmkj.core.api.juhe.bean.JuHeResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述: 聚合数据API
 * 作者: syx
 * 时间: 2016/11/29 14:20
 */
public class JuHeDataApi {

    private static final Logger L = LoggerFactory.getLogger(JuHeDataApi.class);

    public static final String IDCARD_QUERY = "http://op.juhe.cn/idcard/query";
    private static final String IDCARD_KEY = "534572d3d4018b2271c04f1a3590dfdc";

    /**
     * 匹配
     */
    public static final int MATCH = 1;
    /**
     * 不匹配
     */
    public static final int NO_MATCH = 2;
    /**
     * 无法认证
     */
    public static final int CANNOT_VER = 3;
    /**
     * 服务商超时
     */
    public static final int TIMEOUT = 4;

    /**
     * 身份证实名认证 接口
     * 查询身份证与真实姓名是否一致
     *
     * @param realname 真实姓名
     * @param idcard   身份证号码
     * @return int类型 1：匹配 2：不匹配
     * @throws Exception
     */
    public static JuHeResult verifyIdCard(String realname, String idcard) throws Exception {
        realname = realname.trim();
        idcard = idcard.trim();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("key", IDCARD_KEY);
            params.put("idcard", idcard);
            params.put("realname", realname);
            long s1 = System.currentTimeMillis();
            String json = JuHeHttpKit.send(IDCARD_QUERY, params, JuHeHttpKit.POST, "身份证实名认证");
            L.info("调用[身份证实名认证]接口耗时:" + (System.currentTimeMillis() - s1));
            L.info("调用[身份证实名认证]接口返回结果:" + json);
            if (StringUtils.isEmpty(json)){
                L.error("Interface to return data is empty");
            }else {
                JuHeResult result = new JuHeResult(json);
                result.setParams(JSON.toJSONString(params));
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    public static final String VERIFY_BANKCARD4_URL = "http://v.juhe.cn/verifybankcard4/query";
    private static final String VERIFY_BANKCARD4_KEY = "3a6a7f68657c73aa56665a4e48564d03";

    /**
     * 银行卡四元素校验
     *
     * @param realname 真实姓名
     * @param idcard   身份证
     * @param bankcard 银行卡号
     * @param mobile   手机号码
     * @return 验证结果，1:匹配 2:不匹配 3:无法认证 4:服务商超时 -1程序异常  504连接超时
     * @throws Exception
     */
    public static JuHeResult verifyBankCard4(String realname, String idcard, String bankcard, String mobile) throws Exception {
        realname = realname.trim();
        idcard = idcard.trim();
        bankcard = bankcard.trim();
        mobile = mobile.trim();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("key", VERIFY_BANKCARD4_KEY);
            params.put("idcard", idcard);
            params.put("realname", realname);
            params.put("bankcard", bankcard);
            params.put("mobile", mobile);
            long s1 = System.currentTimeMillis();
            String json = JuHeHttpKit.send(VERIFY_BANKCARD4_URL, params, JuHeHttpKit.POST, "银行卡四元素校验");
            L.info("调用[银行卡四元素校验]接口耗时:" + (System.currentTimeMillis() - s1));
            L.info("调用[银行卡四元素校验]接口返回结果:" + json);
            if (StringUtils.isEmpty(json)) {
                L.error("Interface to return data is empty");
            } else {
                JuHeResult result = new JuHeResult(json);
                result.setParams(JSON.toJSONString(params));
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final String DETECTION_BANKCARD_URL = "http://detectionBankCard.api.juhe.cn/bankCard";
    private static final String DETECTION_BANKCARD_KEY = "98b685ef95f1282ed47a6c2fc857672f";

    /**
     * 判断银行卡类型及真伪
     *
     * @param bankCardNo 银行卡号码
     * @return 验证结果，1:匹配 2:不匹配 3:无法认证 4:服务商超时 -1程序异常  504连接超时
     * @throws Exception
     */
    public static JuHeResult detectionBankCard(String bankCardNo) throws Exception {
        bankCardNo = bankCardNo.trim();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("key", DETECTION_BANKCARD_KEY);
            params.put("cardid", bankCardNo);
            long s1 = System.currentTimeMillis();
            String json = JuHeHttpKit.send(DETECTION_BANKCARD_URL, params, JuHeHttpKit.GET, "判断银行卡类型及真伪");
            L.info("调用[判断银行卡类型及真伪]接口耗时:" + (System.currentTimeMillis() - s1));
            L.info("调用[判断银行卡类型及真伪]接口返回结果:" + json);
            if (StringUtils.isEmpty(json)) {
                L.error("Interface to return data is empty");
            } else {
                JuHeResult result = new JuHeResult(json);
                result.setParams(JSON.toJSONString(params));
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final String BANKCARDSILK_URL = "http://bankcardsilk.api.juhe.cn/bankcardsilk/query.php";
    private static final String BANKCARDSILK_KEY = "f0444320a36825b9f84acfaaf3f8142b";

    /**
     * 银行卡类别查询
     *
     * @param bankCardNo 银行卡号码
     * @return 验证结果，1:匹配 2:不匹配 3:无法认证 4:服务商超时 -1程序异常  504连接超时
     * @throws Exception
     */
    public static JuHeResult bankCardSilk(String bankCardNo) throws Exception {
        bankCardNo = bankCardNo.trim();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("key", BANKCARDSILK_KEY);
            params.put("num", bankCardNo);
            long s1 = System.currentTimeMillis();
            String json = JuHeHttpKit.send(BANKCARDSILK_URL, params, JuHeHttpKit.GET, "银行卡类别查询");
            L.info("调用[银行卡类别查询]接口耗时:" + (System.currentTimeMillis() - s1));
            L.info("调用[银行卡类别查询]接口返回结果:" + json);
            if (StringUtils.isEmpty(json)) {
                L.error("Interface to return data is empty");
            } else {
                JuHeResult result = new JuHeResult(json);
                result.setParams(JSON.toJSONString(params));
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws Exception {
//        System.out.println("工商:" + bankCardSilk("6212261202002399443"));
//        System.out.println("平安:" + detectionBankCard("6230580000094652620"));
//        System.out.println("浙江联合银行" + detectionBankCard("6230910199013932146"));
//        System.out.println("工商信用卡" + detectionBankCard("6229100022802284"));
//        System.out.println("工商" + detectionBankCard("6217231202001013769"));
//        System.out.println("邮政:" + detectionBankCard("6217993300047867723"));
        System.out.println(verifyIdCard("王忠说", "111123198811051020"));
//        System.out.println(verifyIdCard("卢楠", "33028119930507872X"));
//        System.out.println(verifyIdCard("毛春千", "22058219921229392X"));
//        System.out.println(verifyIdCard("邵晓旭", "33012419880628492X"));
//        System.out.println(verifyIdCard("朱佳", "320822197901260028"));
//        System.out.println(verifyIdCard("吴顺春","510125195904270421   "));
//        System.out.println(verifyIdCard("蔡余顺","330823197308165538"));
//        System.out.println(verifyIdCard("丁维忠","33042219720411421X"));
//        System.out.println(verifyIdCard("陶国培","330123197107151617"));
//        System.out.println(verifyIdCard("任晓磊","410425198212315517"));
//        System.out.println(verifyIdCard("乔亚明","6230580000091964879"));
//        System.out.println(verifyIdCard("邓辉","230702195912171424"));
//        System.out.println(verifyIdCard("张丽霞","6230580000107580883"));
//        System.out.println(verifyIdCard("邓晓容","522101197408287621"));
//        System.out.println(verifyIdCard("孙超","230702198401171215"));
//        System.out.println(verifyIdCard("彭建锋","43252219731011517X"));
//        System.out.println(verifyIdCard("杨飞球","460200196612280538"));
//        System.out.println(verifyIdCard("彭靖宇","410901196307112728"));
//        System.out.println(verifyIdCard("许思东","430302197205242259"));
//        System.out.println(verifyIdCard("张国华","142202198509292871"));
//        System.out.println(verifyIdCard("何红艳","42092119830819424X"));
//        System.out.println(verifyIdCard("陈秋燕","33032719700514288x"));
//        System.out.println(verifyIdCard("林陈芽","330327196006233164"));

//        System.out.println("结果1:"+verifyBankCard4("吕归红", "362323199009070087", "6230580000110308744", "18868829808"));
//        System.out.println("结果2:"+verifyBankCard4("刘伟", "210212197210060085", "6230580000110307670", "13009423251"));
//        System.out.println("结果3:"+verifyBankCard4("赵莉莉", "330483198512300521", "6217001430009201304", "13857349942"));
//        System.out.println("结果4:"+verifyIdCard("杨蓉", "430623198107128324 "/*, "6230582000028800699", "13360051869"*/));
//        System.out.println("结果5:"+verifyBankCard4("方林华", "330123197908185219", "6230580000110307829", "15868171636"));
//        System.out.println("结果6:"+verifyBankCard4("楼丽姣", "330724197505221026", "6236681460001263397", "18257931908"));
//        System.out.println("结果7:"+verifyBankCard4("王雪芹", "371021197312281023", "6212261614004140155", "13296311165"));
//        System.out.println("结果8:"+verifyBankCard4("贾秀忠", "370723196401200948", "6216914100745438", "13869605542"));
//        System.out.println("结果8:"+verifyBankCard4("陈少琴", "330327196210111586", "6217001420001816613", "18757797788"));

//        System.out.println("中国银行:"+verifyBankCard4("毛春千", "22058219921229392X", "6217856000056844599", "18563962891"));
//        System.out.println("平安银行:"+verifyBankCard4("邵晓旭", "33012419880628492X", "6230580000086320442", "15658000027"));
//        System.out.println("招商银行:"+verifyBankCard4("邵晓旭", "33012419880628492X", "6214835892857457", "15658000027"));
//        System.out.println("交通银行:"+verifyBankCard4("卢楠", "33028119930507872X", "6222620170008619806", "15757162404"));
//        System.out.println("建设银行:"+verifyBankCard4("王忠妍", "220523198811051020", "6217001540007513227", "13616552664"));
//        System.out.println("农业银行:"+verifyBankCard4("朱佳", "320822197901260028", "6228480320736972615", "13958037073"));
    }


}
